
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# DISCLAIMER AND GENERAL INFORMATION
#
# File: App_E_climate_trade_tradeoff.R
# Purpose: Analysis for climate-trade tradeoff dependent variable
# Date: 10 July 2024
# Data: Survey data (pulled through 0_cbam_prep.R)
#
# Technical disclaimer:
# All analyses in R version 4.4.1 (2024-06-14 ucrt) -- "Race for Your Life"
# R Studio 2024.04.2 Build 764 ("Chocolate Cosmos" Release (e4392fc9, 2024-06-05) for Windows)
# Windows 10 Enterprise, 64-bit
# 12th Gen Intel(R) Core(TM) i7-1255U 1.70 GHz with 16GB RAM
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (A) Load data and packages ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Run source file to load data
source("0_cbam_prep.R")

# Load packages
library("tidyverse")
library("janitor")
library("ggpubr")
library("modelsummary")
options(modelsummary_format_numeric_latex = "plain")
library("marginaleffects")
library("xtable")
library("tinytable")

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (B) Regression table for tradeoff results ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Regression models: collapsed treatment conditions
ols_de <- lm(tradeoff_bin~factor(tr_frame), data=dta[dta$country=="germany",])
ols_hu <- lm(tradeoff_bin~factor(tr_frame), data=dta[dta$country=="hungary",])
ols_ch <- lm(tradeoff_bin~factor(tr_frame), data=dta[dta$country=="switzerland",])
ols_uk <- lm(tradeoff_bin~factor(tr_frame), data=dta[dta$country=="uk",])

order_full <- c("factor(tr_frame)2"="Free trade frame",
                "factor(tr_frame)3"="Climate frame")

models.full <- list(
  "Germany" = ols_de,
  "Hungary" = ols_hu,
  "Switzerland" = ols_ch,
  "UK" = ols_uk)


# Produce results table (Table E.1)
tab <- modelsummary(models.full,
                    statistic = 'conf.int',
                    conf_level = .95,
                    coef_rename = order_full,
                    gof_map=c("nobs"),
                    title=c("Regression results for 'climate protection versus trade promotion' trade-off"),
                    output="tinytable") %>%
                    theme_tt("placement", latex_float="h!")

tinytable::save_tt(tab, "./main_cbam_tradeoff.tex", overwrite=TRUE)


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (C) Distribution of climate-trade trade-off variable ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


# Create control group data set
control.dta <- dta[dta$exp_group_fct=="Control",c("country","tradeoff")]
table(control.dta$country)

# Tabulate CBAM support across countries
g <- control.dta %>%
  group_by(country) %>%
  group_map(~tabyl(.x$tradeoff))
names(g) <- unique(dta$country)
g


# Construct plotting data
df.plot <- as.data.frame(do.call(rbind,g))
colnames(df.plot)[1] <- c("tradeoff")
df.plot$country <- str_sub(rownames(df.plot), end=-3)

# Color settings: colorblind-friendly palette
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")


# Plot CBAM support for control condition across four countries
q <- ggplot(df.plot) + 
  geom_col(aes(x=factor(tradeoff), y=percent, fill=country, group=country), position="dodge") +
  labs(y="Percent", title="Climate protection vs trade promotion trade-off by country") +
  theme_minimal() +
  theme(legend.position="bottom") +
  scale_x_discrete(name="Climate-trade trade-off", breaks=c("1","2","3","4","5"),
                   labels=c("Climate protection more important", "", "Both equally important", "", "Trade promotion more important")) + 
  scale_fill_manual(values=c(cols[1],cols[2],cols[3],cols[4]), name="", labels=c("Germany", "Hungary", "Switzerland", "UK"))
q

# Export plot to PDF (Figure E.1)
ggexport(q, filename="./DV_tradeoff.pdf", width=10, height=5)



# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (D) Main results by policy frame ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Regression models: all conditions
ols_de <- lm(tradeoff_bin~factor(exp_group), data=dta[dta$country=="germany",]) 
ols_hu <- lm(tradeoff_bin~factor(exp_group), data=dta[dta$country=="hungary",]) 
ols_ch <- lm(tradeoff_bin~factor(exp_group), data=dta[dta$country=="switzerland",]) 
ols_uk <- lm(tradeoff_bin~factor(exp_group), data=dta[dta$country=="uk",]) 


order_full <- c("factor(exp_group)12"="Climate frame:\n Both",
                "factor(exp_group)11"="Climate frame:\n Prices",
                "factor(exp_group)10"="Climate frame:\n Jobs",
                "factor(exp_group)9"="Climate frame:\n Control",
                "factor(exp_group)8"="Trade frame:\n Both",
                "factor(exp_group)7"="Trade frame:\n Prices", 
                "factor(exp_group)6"="Trade frame:\n Jobs",
                "factor(exp_group)5"="Trade frame:\n Control",
                "factor(exp_group)4"="Both",
                "factor(exp_group)3"="Prices",
                "factor(exp_group)2"="Jobs")

order_full2 <- c("factor(exp_group)12"="Climate frame:\n Both",
                 "factor(exp_group)8"="Trade frame:\n Both",
                 "factor(exp_group)4"="Both",
                 "factor(exp_group)11"="Climate frame:\n Prices",                
                 "factor(exp_group)7"="Trade frame:\n Prices",                 
                 "factor(exp_group)3"="Prices",
                 "factor(exp_group)10"="Climate frame:\n Jobs",
                 "factor(exp_group)6"="Trade frame:\n Jobs",
                 "factor(exp_group)2"="Jobs")


models.full <- list(
  "Germany" = ols_de,
  "Hungary" = ols_hu,
  "Switzerland" = ols_ch,
  "UK" = ols_uk)

# Plot coefficients

# Set color-blind friendly color palatte
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

p1 <- modelplot(models.full, coef_map=order_full2, coef_omit="Interc", conf_level = 0.95) + facet_grid(~model) +
  labs(x="Coefficients",
       y="Experimental conditions",
       title="Treatment Effects across Countries",
       subtitle="DV: Climate protection vs trade promotion (0/1 scale)") +
  geom_vline(xintercept=0, color='black', linetype = "dashed") +
  theme_classic() +
  scale_color_manual(values = cols) +
  theme(legend.title=element_blank()) +
  theme(legend.position="none")
p1


# Export plot to PDF (Figure E.2)
ggexport(p1, filename="./Exp_tradeoff_byframe.pdf", width=10, height=5)

# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (E) Absence of framing effects ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Germany
de <- data.frame(Germany = 
                   # Framing effects for jobs treatment
                   c(hypotheses(ols_de, "b2=b6")$p.value[1], 
                     hypotheses(ols_de, "b2=b10")$p.value[1], 
                     hypotheses(ols_de, "b6=b10")$p.value[1],
                     # Framing effects for prices treatment
                     hypotheses(ols_de, "b3=b7")$p.value[1], 
                     hypotheses(ols_de, "b3=b11")$p.value[1], 
                     hypotheses(ols_de, "b7=b11")$p.value[1],
                     # Framing effects for compound treatment
                     hypotheses(ols_de, "b4=b8")$p.value[1], 
                     hypotheses(ols_de, "b4=b12") $p.value[1],
                     hypotheses(ols_de, "b8=b12")$p.value[1]))

# Hungary
hu <- data.frame(Hungary = 
                   # Framing effects for jobs treatment
                   c(hypotheses(ols_hu, "b2=b6")$p.value[1], 
                     hypotheses(ols_hu, "b2=b10")$p.value[1], 
                     hypotheses(ols_hu, "b6=b10")$p.value[1],
                     # Framing effects for prices treatment
                     hypotheses(ols_hu, "b3=b7")$p.value[1], 
                     hypotheses(ols_hu, "b3=b11")$p.value[1], 
                     hypotheses(ols_hu, "b7=b11")$p.value[1],
                     # Framing effects for compound treatment
                     hypotheses(ols_hu, "b4=b8")$p.value[1], 
                     hypotheses(ols_hu, "b4=b12") $p.value[1],
                     hypotheses(ols_hu, "b8=b12")$p.value[1]))

# Switzerland
ch <- data.frame(Switzerland = 
                   # Framing effects for jobs treatment
                   c(hypotheses(ols_ch, "b2=b6")$p.value[1], 
                     hypotheses(ols_ch, "b2=b10")$p.value[1], 
                     hypotheses(ols_ch, "b6=b10")$p.value[1],
                     # Framing effects for prices treatment
                     hypotheses(ols_ch, "b3=b7")$p.value[1], 
                     hypotheses(ols_ch, "b3=b11")$p.value[1], 
                     hypotheses(ols_ch, "b7=b11")$p.value[1],
                     # Framing effects for compound treatment
                     hypotheses(ols_ch, "b4=b8")$p.value[1], 
                     hypotheses(ols_ch, "b4=b12") $p.value[1],
                     hypotheses(ols_ch, "b8=b12")$p.value[1]))

# UK
uk <- data.frame(UK = 
                   # Framing effects for jobs treatment
                   c(hypotheses(ols_uk, "b2=b6")$p.value[1], 
                     hypotheses(ols_uk, "b2=b10")$p.value[1], 
                     hypotheses(ols_uk, "b6=b10")$p.value[1],
                     # Framing effects for prices treatment
                     hypotheses(ols_uk, "b3=b7")$p.value[1], 
                     hypotheses(ols_uk, "b3=b11")$p.value[1], 
                     hypotheses(ols_uk, "b7=b11")$p.value[1],
                     # Framing effects for compound treatment
                     hypotheses(ols_uk, "b4=b8")$p.value[1], 
                     hypotheses(ols_uk, "b4=b12") $p.value[1],
                     hypotheses(ols_uk, "b8=b12")$p.value[1]))

table <- cbind(de,hu,ch,uk)

# Table E.2
p <- xtable(table, label="app:order_tradeoff", caption="Test statistic p-values for difference-in-means tests for policy frames")
print.xtable(p, booktabs=TRUE, caption.placement="top")

# Difference-in-means tests for control group
hypotheses(ols_de, "b5=b9")
hypotheses(ols_ch, "b5=b9")
hypotheses(ols_uk, "b5=b9")


# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# (F) Tradeoff results as a function of CBAM support ----
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



# Regression models: all conditions
ols_de0 <- lm(tradeoff_bin~factor(tr_main), data=dta[dta$country=="germany" & dta$cbam_support<3,]) 
ols_de1 <- lm(tradeoff_bin~factor(tr_main), data=dta[dta$country=="germany" & dta$cbam_support>3,]) 
ols_hu0 <- lm(tradeoff_bin~factor(tr_main), data=dta[dta$country=="hungary" & dta$cbam_support<3,])
ols_hu1 <- lm(tradeoff_bin~factor(tr_main), data=dta[dta$country=="hungary" & dta$cbam_support>3,]) 
ols_ch0 <- lm(tradeoff_bin~factor(tr_main), data=dta[dta$country=="switzerland" & dta$cbam_support<3,])
ols_ch1 <- lm(tradeoff_bin~factor(tr_main), data=dta[dta$country=="switzerland" & dta$cbam_support>3,]) 
ols_uk0 <- lm(tradeoff_bin~factor(tr_main), data=dta[dta$country=="uk" & dta$cbam_support<3,]) 
ols_uk1 <- lm(tradeoff_bin~factor(tr_main), data=dta[dta$country=="uk" & dta$cbam_support>3,]) 

order_full <- c("factor(tr_main)2"="Treatment: Jobs",
                "factor(tr_main)3"="Treatment: Prices",
                "factor(tr_main)4"="Treatment: Both")  


models.full <- list(
  "Germany: oppose CBAM" = ols_de0,
  "Germany: support CBAM" = ols_de1,
  "Hungary: oppose CBAM" = ols_hu0,
  "Hungary: support CBAM" = ols_hu1,
  "Switzerland: oppose CBAM" = ols_ch0,
  "Switzerland: support CBAM" = ols_ch1,
  "UK: oppose CBAM" = ols_uk0,
  "UK: support CBAM" = ols_uk1)

# Plot coefficients

# Set color-blind friendly color palatte
# http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/#a-colorblind-friendly-palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

p1 <- modelplot(rev(models.full), coef_map=order_full, coef_omit="Interc", conf_level=0.834, facet=TRUE) +
  labs(x="Coefficients",
       title="Treatment Effects across Countries",
       subtitle="DV: Climate protection vs trade promotion (0/1 scale)") +
  geom_vline(xintercept=0, color='black', linetype = "dashed") +
  theme_classic()
p1

# Export plot to PDF (Figure E.3)
ggexport(p1, filename="./Exp_tradeoff_CBAM_support.pdf", width=5, height=7)



# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                         END OF FILE
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
